package org.jeecg.modules.recharge.controller;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.FillRuleUtil;
import org.jeecg.modules.base.controller.JeecgController;
import org.jeecg.modules.discount.entity.Discount;
import org.jeecg.modules.discount.service.IDiscountService;
import org.jeecg.modules.recharge.entity.OrderRecharge;
import org.jeecg.modules.recharge.service.IOrderRechargeService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 充值订单
 * @Author: jeecg-boot
 * @Date:   2024-06-18
 * @Version: V1.0
 */
@Api(tags="充值订单")
@RestController
@RequestMapping("/recharge/orderRecharge")
@Slf4j
public class OrderRechargeController extends JeecgController<OrderRecharge, IOrderRechargeService> {
	@Autowired
	private IOrderRechargeService orderRechargeService;
     @Autowired
     private IDiscountService discountService;

	 /**
	 * 分页列表查询
	 *
	 * @param orderRecharge
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "充值订单-分页列表查询")
	@ApiOperation(value="充值订单-分页列表查询", notes="充值订单-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<OrderRecharge>> queryPageList(OrderRecharge orderRecharge,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<OrderRecharge> queryWrapper = QueryGenerator.initQueryWrapper(orderRecharge, req.getParameterMap());
		String str = req.getParameter("containsBack");
		if(StringUtils.equals(str,"0")){
			queryWrapper.isNotNull("order_num");
		}
		Page<OrderRecharge> page = new Page<OrderRecharge>(pageNo, pageSize);

		IPage<OrderRecharge> pageList = orderRechargeService.pageList(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param orderRecharge
	 * @return
	 */
	@AutoLog(value = "充值订单-添加")
	@ApiOperation(value="充值订单-添加", notes="充值订单-添加")
	//@RequiresPermissions("org.jeecg.modules:sr_order_recharge:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody OrderRecharge orderRecharge) {
		orderRechargeService.save(orderRecharge);
		return Result.OK("添加成功！");
	}

	 @AutoLog(value = "充值订单")
	 @ApiOperation(value="充值订单-添加", notes="充值订单-添加")
	 //@RequiresPermissions("org.jeecg.modules:sr_order_recharge:add")
	 @PostMapping(value = "/create")
	 public Result<OrderRecharge> createRecharge(@RequestBody Discount discount) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		Discount dis = discountService.getById(discount.getId());
		OrderRecharge order = new OrderRecharge();
		order.setSysUserId(sysUser.getId());
	 	order.setOrderNum("" + FillRuleUtil.executeRule("shop_order_num", new JSONObject()));
		order.setOrderName("充值"+dis.getRechargeAmount()+"元，赠送"+dis.getDiscountAmount()+"元");
		order.setRechargeTotal(dis.getRechargeAmount().add(dis.getDiscountAmount()).stripTrailingZeros().toPlainString());
		order.setAmountTotal(dis.getRechargeAmount().stripTrailingZeros().toPlainString());
		order.setPayStatus("0");
		orderRechargeService.save(order);
		return Result.OK(order);
	 }
	/**
	 *  编辑
	 *
	 * @param orderRecharge
	 * @return
	 */
	@AutoLog(value = "充值订单-编辑")
	@ApiOperation(value="充值订单-编辑", notes="充值订单-编辑")
	//@RequiresPermissions("org.jeecg.modules:sr_order_recharge:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody OrderRecharge orderRecharge) {
		orderRechargeService.updateById(orderRecharge);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "充值订单-通过id删除")
	@ApiOperation(value="充值订单-通过id删除", notes="充值订单-通过id删除")
	//@RequiresPermissions("org.jeecg.modules:sr_order_recharge:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		orderRechargeService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "充值订单-批量删除")
	@ApiOperation(value="充值订单-批量删除", notes="充值订单-批量删除")
	//@RequiresPermissions("org.jeecg.modules:sr_order_recharge:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.orderRechargeService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "充值订单-通过id查询")
	@ApiOperation(value="充值订单-通过id查询", notes="充值订单-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<OrderRecharge> queryById(@RequestParam(name="id",required=true) String id) {
		OrderRecharge orderRecharge = orderRechargeService.getById(id);
		if(orderRecharge==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(orderRecharge);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param orderRecharge
    */
    //@RequiresPermissions("org.jeecg.modules:sr_order_recharge:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, OrderRecharge orderRecharge) {
		String exportFieldsList = this.getExportFields(OrderRecharge.class,"rechargeorder:");
        return super.exportXls(request, orderRecharge, OrderRecharge.class, "充值订单",exportFieldsList);
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("sr_order_recharge:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, OrderRecharge.class);
    }

}
